Just as we can perform relational operations on scalars, we can also
do matrix relational operations. When we do a relational test on
two matrices, such as A == B
, the operands must be the same
dimensions, or one of them must be a 1-by-1 matrix. The result of
a matrix relational test is a matrix the same size as the operands
filled with ones and zeros according to the result of an
element-by-element test. A one in a particular location means the
test was true for the pair of elements in those locations in the
operands. For example:
> a = [1, 2, 3; 4, 5, 6; 7, 8, 9]
a =
matrix columns 1 thru 3
1 2 3
4 5 6
7 8 9
> b = a'
b =
matrix columns 1 thru 3
1 4 7
2 5 8
3 6 9
> a == b
matrix columns 1 thru 3
1 0 0
0 1 0
0 0 1
> a >= 5
matrix columns 1 thru 3
0 0 0
0 1 1
1 1 1
if-tests do not accept matrices. The any()
and
all()
functions can be used in combination with relational
and logical tests to conditionally execute statements based upon
matrix properties.
The function any()
returns true if any of the elements of
it's argument are non-zero. The function all()
returns true
if all of the elements of it's argument are non-zero. The any()
function is called on the output of another any()
call because the
any()
function returns a vector when passed a matrix that is not
already a vector.